package com.lti.appnet.mongodb.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lti.appnet.mongodb.domain.BaseDomain;
import com.lti.appnet.mongodb.domain.Counter;
import com.lti.appnet.mongodb.respository.DBIdRepository;
@Service
public class IdIncrementEvent {
	@Autowired
	private DBIdRepository dbIdRepository;
	
	public void incrementIdSetting(BaseDomain baseDomain){
		if (baseDomain.getID() == null) {
			String clazz = baseDomain.get_class();

			Counter counter = dbIdRepository.findByClazz(clazz);
			if (counter == null) {
				counter = new Counter();
				counter.setClazz(baseDomain.get_class());
				counter.setCount(1);
				dbIdRepository.save(counter);
				baseDomain.setID("1");
			} else {
				long count = counter.getCount();
				count++;
				counter.setCount(count);
				dbIdRepository.save(counter);
				baseDomain.setID(String.valueOf(count));
			}
		}
	}

}
